<?php
declare (strict_types=1);

namespace app\admin\controller\admin;

use app\admin\annotation\common\Auth;
use app\admin\service\admin\FileService;
use app\common\Constants;
use think\App;
use think\facade\Log;
use app\admin\controller\Admin;
use app\admin\model\admin\File as FileModel;
use app\admin\validate\admin\File as FileValidate;


class File extends Admin
{

    public function __construct(
        App                 $app,
        protected FileModel $fileModel,
    )
    {
        parent::__construct($app);
    }

    #[Auth('admin:file:index')]
    public function index(): \think\response\Json
    {
        try {
            $params = $this->request->post();
            validate(FileValidate::class)
                ->scene('index')
                ->check($params);
            $query = $this->fileModel;
            // 弹窗或者跳转过滤数据
            $query->when(isset($params['id']), function ($query) use ($params) {
                $query->where('file_id', '=', $params['id']);
            });
            $query->when(isset($params['keyword']), function ($query) use ($params) {
                $keyword = str_replace(['%', '_'], ['\%', '\_'], $params['keyword']);
                $query->where('name|mime', 'like', "%" . $keyword . "%");
            });
            $query->when(isset($params['extension']), function ($query) use ($params) {
                $query->whereLike('extension', '%' . $params['extension'] . '%');
            });
            $query->when(isset($params['create_time']), function ($query) use ($params) {
                $query->where('create_time', 'between', $params['create_time']);
            });
            // 排序
            $order_by = sort_rules($params['order_by'] ?? 'sort|asc');
            $query->order($order_by);
            // 分页
            $page_size = $params['page_size'] ?? 20;
            $current_page = $params['current_page'] ?? 1;
            $list = $query->paginate(['list_rows' => $page_size, 'page' => $current_page])->toArray();
            $list['sort'] = $order_by;
            return $this->success(['data' => $list]);
        } catch (\Exception $e) {
            throw $e;
        }
    }


    #[Auth('admin:file:add')]
    public function add(): \think\response\Json
    {
        $file = $this->request->file('file');
        // 检查上传的文件是否符合要求
        FileService::validateFileType($file);
        // 查询是否已经存在相同的文件，如果有就不再上传了
        $fileInfo = FileService::getSameFileUrl($file);
        // 如果返回了相同的文件地址，则直接返回，否则执行查询
        if ($fileInfo) {
            return $this->success(['data' => $fileInfo]);
        } else {
            return $this->success(['data' => FileService::doUpload($file)]);
        }
    }

    #[Auth('admin:file:update')]
    public function update(): \think\response\Json
    {
        $fields = "id,name";
        $data = $this->request->only(explode(',', $fields), 'post');
        validate(FileValidate::class)
            ->scene('update')
            ->check($data);
        FileModel::update($data);
        return $this->success();
    }


    #[Auth('admin:file:delete')]
    public function delete(): \think\response\Json
    {
        $ids = $this->request->post('id/a');
        validate(FileValidate::class)
            ->scene('delete')
            ->check(['ids' => $ids]);
        $count = FileModel::destroy($ids);
        return $this->success(['data' => $count]);
    }

}
